﻿// JScript 文件

function CModel()
{
    //存放目录列表 {ID, Name, Path, ParentID, DirL<>, FileL<>}
    this.DiryList = {};
    this.DiryCount = 0;
    
    this.m_lastObject ={};
    
    //加载第一层目录信息
    this.Init = function()
    {
        this.m_lastObject = {DirID:0, DirName:"", ParentDirID:0, tol:0 ,nCurPage:1,nPageSize:8, DelID:"", DelName:""};
        
        var response = ListFiles.GetRoot();
        eval(response.value);
        this.DiryCount += this.DiryList[0].DirList.length;
        
        //显示左边菜单
        CViewMgr.sm_viewMgr.ShowLeftMenu(this.m_lastObject, this.DiryList[0]);
    }
    
    //取得子目录
    this.GetSubDirList = function(object)
    {
        this.m_lastObject = object;
        objCurrDirInfo = this.DiryList[this.m_lastObject.DirID];
        if(objCurrDirInfo=='undefined'|| objCurrDirInfo==null)
        {
            var objDirInfo = this.DiryList[this.m_lastObject.ParentDirID];
            if(objDirInfo)
            {
                var response = ListFiles.GetSubDirectoryList(this.m_lastObject.DirID, this.m_lastObject.DirName, objDirInfo.ID, objDirInfo.Path  + "\\" + this.m_lastObject.DirName, this.DiryCount);
                eval(response.value);
                this.DiryCount += this.DiryList[this.m_lastObject.DirID].DirList.length;
        
                //显示左边菜单
                CViewMgr.sm_viewMgr.ShowLeftMenu(this.m_lastObject, this.DiryList[this.m_lastObject.DirID]);
            }
            else
            {
                //非法
            }
        }
    }
    
    /*    
        获得数据分页记录 第一页 nCurPage 为 1;
    */    
    this.GetCurPageData = function (object)
    {
        this.m_lastObject = object;
        var nCurPage = this.m_lastObject.nCurPage;
        var nPageSize = this.m_lastObject.nPageSize;
        var record = {ID:0, Name:"", Path:"", ParentID:"", FileList:{}};
        
        //当前目录的数组
        var objDir = this.DiryList[this.m_lastObject.DirID];
        
        //取出对应页面的数据.
        record.ID = objDir.ID;
        record.Name = objDir.Name;
        record.Path = objDir.Path;
        record.ParentID = objDir.ParentID;
        record.nCurCount = 0;
        
        //总的文件个数
        var fCount = objDir.FileList.length;
        this.m_lastObject.tol = fCount;
        
        //开始下标
        var nBegin = (nCurPage - 1) * nPageSize;
        
        //结束时的下标
        var nEnd = nCurPage * nPageSize > fCount ?  fCount : nCurPage * nPageSize ;
        
        for(var i=0; nBegin < objDir.FileList.length, nBegin < nEnd ; i++,nBegin++)
        {
            record.nCurCount ++;
            record.FileList[i] = objDir.FileList[nBegin];
        }
        //显示数据
        CViewMgr.sm_viewMgr.ShowPanel(this.m_lastObject, record);
    }
    
    
    //单击左菜单项文字
    this.OnClickMenuItemText = function(object)
    {
        this.GetSubDirList(object);
        object.nCurPage = 1;
        this.GetCurPageData(object);
    }
    
    //单击分页
    this.OnClickPageItem = function(object)
    {
        this.GetCurPageData(object);
    }
    
    //删除图片
    this.OnClickDelItem = function(object, strPath, strID)
    {
        this.m_lastObject = object;
        var strID = this.m_lastObject.DelID;
        var strPath = this.m_lastObject.DelName;
        var response = ListFiles.DeleteFileInfo(strPath);
        if(response.value == "1")
        {
            //当前目录的数组
            var objDir = this.DiryList[this.m_lastObject.DirID];
            var arrFiles = objDir.FileList;
            if(arrFiles!='undefined' && arrFiles!=null)
            {
                for(var i=0; i<arrFiles.length;i++)
                {
                    if(arrFiles[i].ID == strID)
                    {
                        this.DiryList[this.m_lastObject.DirID].FileList.splice(i, 1);
                        break;
                    }
                }
            }
            this.GetCurPageData(object);
        }
        else
        {
            alert("删除失败");
        }
    }
}